package com.eric.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.eric.Config;
import com.eric.downloader.SimplePageDownloader;

/**
 * 通用数据读取类 可从命令行，文件，网络读取 不可做蜘蛛用，如要蜘蛛，使用com.eric.downloader.SimplePageDownloader
 * 
 * @author Eric
 *
 */
public class Reader {

	private static String inputType;

	public static Set<String> read2Set(String input) {
		Set<String> set = new HashSet<>();
		set.addAll(read2List(input));
		return set;
	}

	public static List<List<String>> read2ListArrays(String input) {
		List<String> lines = read2List(input);
		List<List<String>> listArray = new ArrayList<List<String>>();

		ArrayList<String> lineList;
		for (String s : lines) {
			lineList = new ArrayList<>();
			String[] ss = s.split(",");
			for (String sss : ss) {
				lineList.add(sss);
			}
			listArray.add((List<String>) lineList);
		}
		return listArray;
	}

	/**
	 * @deprecated
	 * @param input
	 * @return
	 */
	public static String read2String(String input) {
		StringBuilder sb = new StringBuilder();
		BufferedReader br;
		String line;
		try {
			br = new BufferedReader(new InputStreamReader(read2Stream(input)));
			while ((line = br.readLine()) != null) {
				sb.append(line);
				sb.append(System.lineSeparator());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * @deprecated
	 * @param input
	 * @return
	 */
	public static InputStream read2Stream(String input) {
		File file = new File(input);
		InputStream is = null;
		if (file.exists()) {
			try {
				is = new FileInputStream(file);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		} else if (input.startsWith("http")) {
			URL url;
			try {
				url = new URL(input);
				is = url.openConnection().getInputStream();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			is = new ByteArrayInputStream(input.getBytes());
		}

		return is;
	}

	public static String inputType() {
		return inputType;
	}

	public static Document read2doc(Object input) {
		Document doc = null;
		if (input instanceof File) {// 输入为File
			inputType = Config.INPUT_TYPE.FILE.name().toLowerCase();
			try {
				doc = Jsoup.parse((File) input, Config.DEFAULT_CHARSET);
				doc.setBaseUri("");
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (input instanceof String && (((String) input)).startsWith("http")) {// 输入为String格式的网址
			inputType = Config.INPUT_TYPE.STR_URL.name().toLowerCase();
			SimplePageDownloader spd = new SimplePageDownloader();
			doc = spd.download((String) input).document();

			doc.setBaseUri((String) input);
		} else if (input instanceof URL) {// 输入为URL
			inputType = Config.INPUT_TYPE.URL.name().toLowerCase();
			SimplePageDownloader spd = new SimplePageDownloader();
			doc = spd.download((URL) input).document();
			doc.setBaseUri(((URL) input).toString());
		} else if (input instanceof Document) {
			inputType = Config.INPUT_TYPE.DOC.name().toLowerCase();
			doc = (Document) input;
		} else if (input instanceof String && !(((String) input)).startsWith("http")) {// 输入为String，进一步判断
			File file = new File((String) input);
			if (file.exists()) {
				inputType = Config.INPUT_TYPE.STR_FILE.name().toLowerCase();
				doc = read2doc(new File((String) input));// 判断为File
				doc.setBaseUri("");
			} else {
				inputType = Config.INPUT_TYPE.STRING.name().toLowerCase();
				doc = Jsoup.parseBodyFragment((String) input);// 判断为一段文字
			}
		}
		return doc;
	}

	public static String read2String(Object input) {
		StringBuilder sb = new StringBuilder();
		if (input instanceof File) {// 输入为File
			inputType = Config.INPUT_TYPE.FILE.name().toLowerCase();

			BufferedReader br;
			try {
				br = new BufferedReader(new FileReader((File) input));
				String line;
				while ((line = br.readLine()) != null) {
					sb.append(line);
					sb.append(System.lineSeparator());
				}
				br.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

		} else if (input instanceof String && UrlUtils.isUrl((String) input)) {// 输入为String格式的网址
			inputType = Config.INPUT_TYPE.STR_URL.name().toLowerCase();
			SimplePageDownloader spd = new SimplePageDownloader();
			String text = spd.download((String) input).document().html();
			sb.append(text);
		} else if (input instanceof URL) {// 输入为URL
			inputType = Config.INPUT_TYPE.URL.name().toLowerCase();
			SimplePageDownloader spd = new SimplePageDownloader();
			String text = spd.download((URL) input).document().html();
			sb.append(text);
		} else if (input instanceof Document) {
			inputType = Config.INPUT_TYPE.DOC.name().toLowerCase();
			sb.append(((Document) input).html());
		} else if (input instanceof String && !UrlUtils.isUrl((String) input)) {// 输入为String，进一步判断
			File file = new File((String) input);
			if (file.exists()) {
				inputType = Config.INPUT_TYPE.STR_FILE.name().toLowerCase();
				return read2String(new File((String) input));// 判断为File
			} else {
				inputType = Config.INPUT_TYPE.STRING.name().toLowerCase();
				return ((String) input);// 判断为一段文字
			}
		}
		return sb.toString();
	}

	public static List<String> read2List(Object input) {
		List<String> list = new ArrayList<>();
		if (input instanceof File) {// 输入为File
			inputType = Config.INPUT_TYPE.FILE.name().toLowerCase();
			try {
				BufferedReader br = new BufferedReader(new FileReader((File) input));
				String line;
				while ((line = br.readLine()) != null) {
					list.add(line);
				}
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (input instanceof String && ((String) input).startsWith("http")) {// 输入为String格式的网址
			inputType = Config.INPUT_TYPE.STR_URL.name().toLowerCase();
			SimplePageDownloader spd = new SimplePageDownloader();
			String[] s = spd.download((String) input).html().split("\n");
			for (String ss : s) {
				list.add(ss);
			}

		} else if (input instanceof URL) {// 输入为URL
			inputType = Config.INPUT_TYPE.URL.name().toLowerCase();
			SimplePageDownloader spd = new SimplePageDownloader();
			String[] s = spd.download((URL) input).html().split("\n");
			for (String ss : s) {
				list.add(ss);
			}
		} else if (input instanceof Document) {
			inputType = Config.INPUT_TYPE.DOC.name().toLowerCase();
			Document doc = (Document) input;
			String[] s = doc.html().split("\n");
			for (String ss : s) {
				list.add(ss);
			}
		} else if (input instanceof String && !((String) input).startsWith("http")) {// 输入为String，进一步判断
			File file = new File((String) input);
			if (file.exists()) {
				inputType = Config.INPUT_TYPE.STR_FILE.name().toLowerCase();
				list = read2List(new File((String) input));// 判断为File
			} else {
				inputType = Config.INPUT_TYPE.STRING.name().toLowerCase();
				list.add((String) input);// 判断为一段文字
			}
		}

		return list;
	}

}
